package com.wtao.testcamera;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.util.VLCVideoLayout;

/**
 *
 */
public class VLCRtspActivity extends AppCompatActivity {
    String TAG = "RtspActivity";
    private VLCVideoLayout videoLayout;
    private LibVLC libVlc;
    private MediaPlayer mediaPlayer;

    private EditText etUrl;
    private Boolean flag = true;
    String url = null;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtsp_vlc);
        videoLayout = findViewById(R.id.vlcVideoPlay);
        etUrl = findViewById(R.id.etUrl);
        url = etUrl.getText().toString();

        libVlc = new LibVLC(this);
        mediaPlayer = new MediaPlayer(libVlc);
        findViewById(R.id.btnBack).setOnClickListener(v->{
            finish();
        });
    }


    @Override
    protected void onStart() {
        super.onStart();
        mediaPlayer.attachViews(videoLayout, null, false, false);
        Media media = new Media(libVlc, Uri.parse(url));
        media.setHWDecoderEnabled(true, false);
        media.addOption(":network-caching=600");

        mediaPlayer.setMedia(media);
        media.release();
        mediaPlayer.play();
    }

    @Override
    protected void onStop() {
        super.onStop();

        mediaPlayer.stop();
        mediaPlayer.detachViews();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mediaPlayer.release();
        libVlc.release();
    }
}